package com.iaz.tech.tools.property.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.property.IPropertyAble;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = true)
@Data
public class Properties extends Property {
	
	/**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(Properties.class);
	
	/**
	 * 内部子Properties
	 */
	private List<IPropertyAble> properties = new ArrayList<>();
	
	/**
	 * 增加新的成员
	 * @param child
	 */
	public void append(IPropertyAble child) {
		try {
			if (child == null) {
				throw new Exception("入口参数child 为空，或者没有指定。");
			}
			if (this.properties == null) {
				this.properties = new ArrayList<IPropertyAble>();
			}
			
			this.properties.add(child);
			
		} catch(Exception e) {
			logger.error( "异常：" + e.getMessage());
		}
	}
	
	public void insert(int index, IPropertyAble child) {
		try {
			if (child == null) {
				throw new Exception("入口参数child 为空，或者没有指定。");
			}
			if (this.properties == null) {
				this.properties = new ArrayList<IPropertyAble>();
			}
			if (index < 0) {
				return ;
			}
			if (index > this.properties.size()) {
				return;
			}
			this.properties.add(index, child);
		} catch(Exception e) {
			logger.error("异常：" + e.getMessage());
		}
	}
	
	public IPropertyAble delete(int index) {
		IPropertyAble result = null;
		try {
			 
			if (this.properties == null) {
				this.properties = new ArrayList<IPropertyAble>();
			}
			if (index < 0) {
				return result;
			}
			if (index > this.properties.size()) {
				return result;
			}
			result = this.properties.remove(index);
			return result;
		} catch(Exception e) {
			logger.error("异常：" + e.getMessage());
		}
		return null;
	}
	
	public void clear() {
		try {
			 
			if (this.properties == null) {
				this.properties = new ArrayList<IPropertyAble>();
			}
			this.properties.clear();
		} catch(Exception e) {
			logger.error("异常：" + e.getMessage());
		}
	}
}
